#include "vector.h"


void Test1()
{
	my_vector::vector<int> arr1(10);//构造一个大小为10的vector对象
	for (int i = 0; i < 10; ++i)
		cout << arr1[i] << endl;
	int arr2[10];
	for (int i = 0; i < 10; ++i)
		cout << arr2[i] << endl;
}

typedef int* pf;
void Test2()//内置类型的匿名对象
{
	cout << int() << endl;
	cout << float() << endl;
	cout << pf() << endl;
	cout << char() << endl;
	cout << bool() << endl;
}

void Test3()//迭代器访问
{
	my_vector::vector<int> arr(10, 1);//创建vector对象
	my_vector::vector<int>::iterator it = arr.begin();
	while(it != arr.end())//老老实实使用迭代器
	{
		cout << *it << endl;
		it++;
	}

	for (int val: arr)//范围for,交给编译器来做
	{
		cout << val << endl;
	}
}
void Test4()//尾插和尾删
{
	my_vector::vector<int> arr;//创建arr对象
	for (int i = 0; i < 10; ++i)//尾插10个元素
		arr.push_back(i);
	for (auto& val : arr)
		cout << val << " ";
	cout << endl;

	arr.pop_back();//尾删
	arr.pop_back();//尾删
	for (auto& val : arr)
		cout << val << " ";
}

void Test5()//insert
{
	my_vector::vector<int> arr((size_t)5, 1);//创建vector对象
	for (int val : arr)//范围for,交给编译器来做
	{
		cout << val << " ";
	}
	cout << endl;

	arr.insert(arr.begin() + 3, 12);//在arr.begin()+3位置前插入12
	for (int val : arr)//范围for,交给编译器来做
	{
		cout << val << " ";
	}
	cout << endl;

	//arr.insert(arr.begin(),3,-1);//头插3个-1
	//for (int val : arr)//范围for,交给编译器来做
	//{
	//	cout << val << " ";
	//}
	//cout << endl;
	//arr.insert(arr.end(), arr.begin(), arr.begin() + 2);//把arr这两个迭代器范围的值，尾插到arr中
	//for (int val : arr)//范围for,交给编译器来做
	//{
	//	cout << val << " ";
	//}
	//cout << endl;
}

void Test6()//erase
{
	my_vector::vector<int> arr;
	my_vector::vector<int> arr1;
	arr1 = arr;
	for (int i = 0; i < 10; ++i)
		arr.push_back(i);
	for (int val : arr)//范围for,交给编译器来做
	{
		cout << val << " ";
	}
	cout << endl;
	arr.insert(arr.begin(),100);
	arr.erase(arr.end()-1);//尾删
	for (int val : arr)//范围for,交给编译器来做
	{
		cout << val << " ";
	}
	cout << endl;

	//arr.erase(arr.begin(),arr.end()-1);//[begin,end-1)
	//for (int val : arr)//范围for,交给编译器来做
	//{
	//	cout << val << " ";
	//}
	//cout << endl;

}

void Test7()//扩容导致迭代器失效
{
	my_vector::vector<int> arr((size_t)10,-1);
	auto it = arr.begin();
	arr.resize(100, 0);

	it = arr.begin();//更新迭代器

	while(it != arr.end())
	{
		cout << *it << " ";
		it++;
	}
}

void Test8()//删除元素导致迭代器失效
{
	my_vector::vector<int> arr((size_t)10,-1);
	auto it = arr.begin();

	arr.erase(arr.begin());//头删

	//it = arr.begin();//更新

	while (it != arr.end())
	{
		cout << *it << endl;
		it++;
	}
}

void Test9()//reserve
{
	vector<int> arr;
	arr.reserve(10000);
	cout << arr.capacity() << endl;
	arr.reserve(10);
	cout << arr.capacity() << endl;
}

void Test10()//shrink_to_fit
{
	my_vector::vector<int> arr;
	arr.reserve(1000);
	cout << arr.capacity() << endl;
	arr.resize(99);
	arr.shrink_to_fit();
	cout << arr.capacity() << endl;
}

void Test11()//迭代器范围构造
{
	my_vector::vector<int> arr1(10, 1);
	my_vector::vector<int> arr2(arr1.begin(), arr1.end());
	
	for (auto& i : arr2)
		cout << i << " ";
	cout << endl;
	my_vector::vector<int> arr3(11,0);
	for (auto& i : arr3)
		cout << i << " ";
	cout << endl;
	arr3 = arr2;

	for (auto& i : arr3)
		cout << i << " ";
	cout << endl;

	arr2[1] = 100;
	swap(arr1, arr2);
	for (auto& i : arr1)
		cout << i << " ";
	cout << endl;
	arr1.clear();
	cout << arr1.capacity() << " " << arr1.size() << endl;
	cout << arr1.empty() << endl;
}
int main()
{
	Test11();
	return 0;
}